﻿using System;
using System.Web.Configuration;
using ErrorPageZ.Classes;
using ErrorPageZ.Controllers;
using visinia.SmartEngine;

public partial class Admin_Pages_ErrorPageZ_CustomErrorPageZ : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CUSTOM_ERRORS_MODES.SelectedValue = CustomErrorPagesController.GetCustomErrorsMode(Page).ToString();

        CustomErrorPageInfo info = CustomErrorPagesController.GetDefaultRedirect(Page);
        if (info != null)
            CheckBoxDefaultRedirect.Checked = info.PageId != 0 ? true : false;
    }      
    
    protected void CustomErrorPages_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName == "SetErrorPage")
        {
            int index = Convert.ToInt32(e.CommandArgument);            
            int StatusCode = Convert.ToInt32(CustomErrorPages.Rows[index].Cells[0].Text);
            CustomErrorPagesController.SetCustomErrorPage(StatusCode, Page);
            Response.Redirect(Request.RawUrl);
        }
        if (e.CommandName == "ResetErrorPage")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int StatusCode = Convert.ToInt32(CustomErrorPages.Rows[index].Cells[0].Text);
            CustomErrorPagesController.ResetCustomErrorPage(StatusCode, Page);
            Response.Redirect(Request.RawUrl);
        }
        if (e.CommandName == "DesignPage")
        {
            int index = Convert.ToInt32(e.CommandArgument);            
            int StatusCode = Convert.ToInt32(CustomErrorPages.Rows[index].Cells[0].Text);
            CustomErrorPageInfo errorpage = CustomErrorPagesController.GetCustomErrorPage(StatusCode);
            if (errorpage == null)
                Response.Redirect(Request.RawUrl);              
            Response.Redirect(WebRoots.GetWebRoot + "Designer.aspx?PageId=" + errorpage.PageId);
        }
    }
    protected void UpdateMode_Click(object sender, EventArgs e)
    {
        if (CUSTOM_ERRORS_MODES.SelectedValue == CustomErrorsMode.On.ToString())
            CustomErrorPagesController.SetCustomErrorsMode(CustomErrorsMode.On, Page);
        if (CUSTOM_ERRORS_MODES.SelectedValue == CustomErrorsMode.Off.ToString())
            CustomErrorPagesController.SetCustomErrorsMode(CustomErrorsMode.Off, Page);
        if (CUSTOM_ERRORS_MODES.SelectedValue == CustomErrorsMode.RemoteOnly.ToString())
            CustomErrorPagesController.SetCustomErrorsMode(CustomErrorsMode.RemoteOnly, Page);
    }

    protected void CANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }

    protected void SetDefaultRedirect_Click(object sender, EventArgs e)
    {
        CustomErrorPagesController.SetDefaultRedirect(Page);
        Response.Redirect(Request.RawUrl);
    }
    protected void ResetDefaultRedirect_Click(object sender, EventArgs e)
    {
        CustomErrorPagesController.ResetDefaultRedirect(Page);
        Response.Redirect(Request.RawUrl);
    }
    protected void DesignDefaultRedirect_Click(object sender, EventArgs e)
    {
        CustomErrorPageInfo info = CustomErrorPagesController.GetDefaultRedirect(Page);
        if (info != null)
        {
            Response.Redirect(WebRoots.GetWebRoot + "Designer.aspx?PageId=" + info.redirect);
        }
    }
}
